package org.abettor.tanqiu.gameloop;

import org.abettor.tanqiu.gameloop.object.Ball;
import org.abettor.tanqiu.gameloop.object.BallMap;
import org.abettor.tanqiu.gameloop.object.BallMapPart;
import org.abettor.tanqiu.gameloop.object.BallMapSpherePart;
import org.abettor.tanqiu.gameloop.object.BallMapSquarePart;
import org.abettor.tanqiu.gameloop.object.RailBallContainer;

import android.util.Log;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class SimulationRenderer {
	Texture redBallTexture;// 红色球的纹理
	Texture background;// 背景色
	Texture squarePartTexture;// 正方形的组件
	Texture spherePartTexture;// 椭圆形的组件
	TextureRegion backgroundRegion;

	/**
	 * 构造函数
	 */
	public SimulationRenderer() {
		this.cam = new OrthographicCamera(Constants.WIDTH, Constants.HEIGHT);
		this.cam.position.set(Constants.WIDTH / 2, Constants.HEIGHT / 2, 0);
		spriteBatch = new SpriteBatch();
		redBallTexture = new Texture(Gdx.files.internal("data/boll.png"), true);
		background = new Texture(Gdx.files.internal("data/bg.png"), true);
		backgroundRegion = new TextureRegion(background, 0, 0, 320, 480);
		squarePartTexture = new Texture(Gdx.files.internal("data/straight.png"));
		spherePartTexture = new Texture(
				Gdx.files.internal("data/half_quarter.png"));
	}

	// 批量处理的精灵类
	SpriteBatch spriteBatch;
	OrthographicCamera cam;

	/**
	 * dispose的操作
	 */
	public void dispose() {
	}

	public void render(Application app, Simulation simulation) {
		GL10 gl = app.getGraphics().getGL10();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glViewport(0, 0, app.getGraphics().getWidth(), app.getGraphics()
				.getHeight());
		// 进行渲染的操作
		cam.update();
		spriteBatch.setProjectionMatrix(cam.combined);
		renderBack();// 渲染背景图
		renderMap(simulation);// 渲染地图
		renderRailBall(simulation);
	}

	// 渲染背景图片
	private void renderBack() {
		spriteBatch.begin();
		spriteBatch.draw(backgroundRegion,
				cam.position.x - Constants.WIDTH / 2, cam.position.y
						- Constants.HEIGHT / 2, Constants.WIDTH,
				Constants.HEIGHT);
		spriteBatch.end();
	}

	// 渲染物品的操作
	public void renderObjects() {
		spriteBatch.enableBlending();
		spriteBatch.begin();
		spriteBatch.draw(redBallTexture, 10, 10, 0, 0, 20, 20);
		spriteBatch.end();
	}

	// 渲染地图的操作
	private void renderMap(Simulation simulation) {
		spriteBatch.enableBlending();
		spriteBatch.begin();
		BallMap map = simulation.getMap();
		for (int i = 0; i < map.size(); i++) {
			BallMapPart part = map.getBallMapPart(i);
			renderMapPart(part);
		}
		spriteBatch.end();
	}

	// 渲染地图部件的操作
	private void renderMapPart(BallMapPart part) {
		if (part instanceof BallMapSpherePart) {
			BallMapSpherePart spherePart = (BallMapSpherePart) part;
			Vector2 start = spherePart.getBegin();
			Vector2 end = spherePart.getEnd();
			Vector2 dot = spherePart.getCircleDot();
			switch (spherePart.getType()) {
			case BallMapSpherePart.TYPE_1:
				if (end.y > start.y) {
					spriteBatch.draw(spherePartTexture, start.x, start.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, 90, 0, 0,
							32, 32, false, false);
				} else {
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, 90, 0, 0,
							32, 32, false, false);
				}
				break;
			case BallMapSpherePart.TYPE_2:
				if (end.y > start.y) {
					spriteBatch.draw(spherePartTexture, start.x, start.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, 45, 0, 0,
							32, 32, false, false);
				} else {
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, 45, 0, 0,
							32, 32, false, false);
				}
				break;
			case BallMapSpherePart.TYPE_3:
				if (end.x > start.x) {
					spriteBatch.draw(spherePartTexture, start.x, start.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, 0, 0, 0,
							32, 32, false, false);
				} else {
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, 0, 0, 0,
							32, 32, false, false);
				}
				break;
			case BallMapSpherePart.TYPE_4:
				if (end.x > start.x) {
					spriteBatch.draw(spherePartTexture, start.x, start.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -45, 0,
							0, 32, 32, false, false);
				} else {
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -45, 0,
							0, 32, 32, false, false);
				}
				break;
			case BallMapSpherePart.TYPE_5:
				if (end.y < start.y) {
					spriteBatch.draw(spherePartTexture, dot.x, dot.y
							, 0,
							0, 32, 32, 1, 1, -90, 0,
							0, 32, 32, false, false);
				}else{
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -90, 0,
							0, 32, 32, false, false);
				}
				break;
			case BallMapSpherePart.TYPE_6:
				if (end.y < start.y) {
					spriteBatch.draw(spherePartTexture, start.x, start.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -135, 0,
							0, 32, 32, false, false);
				} else {
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -135, 0,
							0, 32, 32, false, false);
				}
				break;
			case BallMapSpherePart.TYPE_7:
				if (end.y > start.y) {
					spriteBatch.draw(spherePartTexture, start.x, start.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -180, 0,
							0, 32, 32, false, false);
				} else {
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -180, 0,
							0, 32, 32, false, false);
				}
				break;
			case BallMapSpherePart.TYPE_8:
				if (end.y > start.y) {
					spriteBatch.draw(spherePartTexture, start.x, start.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -235, 0,
							0, 32, 32, false, false);
				} else {
					spriteBatch.draw(spherePartTexture, end.x, end.y
							+ Constants.BALL_RAIDUS - 32, 0,
							32 - Constants.BALL_RAIDUS, 32, 32, 1, 1, -235, 0,
							0, 32, 32, false, false);
				}
				break;
			}
		} else if (part instanceof BallMapSquarePart) {
			BallMapSquarePart squarePart = (BallMapSquarePart) part;
			Vector2 start = squarePart.getBegin();
			Vector2 end = squarePart.getEnd();
			switch (squarePart.getType()) {
			case BallMapSquarePart.TYPE_HORIZONTAL:
				if (end.x > start.x) {
					spriteBatch.draw(squarePartTexture, start.x, start.y
							- Constants.BALL_RAIDUS, 0, 0,
							squarePart.getLenth(), Constants.SQUAREPART_WIDTH,
							1, 1, 0, 0, 0, 16, 16, false, false);
				} else {
					spriteBatch.draw(squarePartTexture, end.x, end.y
							- Constants.BALL_RAIDUS, 0, 0,
							squarePart.getLenth(), Constants.SQUAREPART_WIDTH,
							1, 1, 0, 0, 0, 16, 16, false, false);
				}
				break;
			case BallMapSquarePart.TYPE_LEFTSLANT:
				if (end.x > start.x) {
					spriteBatch.draw(squarePartTexture, start.x, start.y
							- Constants.BALL_RAIDUS, 0, Constants.BALL_RAIDUS,
							squarePart.getLenth(), Constants.SQUAREPART_WIDTH,
							1, 1, 45, 0, 0, 16, 16, false, false);
				} else {
					spriteBatch.draw(squarePartTexture, end.x, end.y
							- Constants.BALL_RAIDUS, 0, Constants.BALL_RAIDUS,
							squarePart.getLenth(), Constants.SQUAREPART_WIDTH,
							1, 1, 45, 0, 0, 16, 16, false, false);
				}
				break;
			case BallMapSquarePart.TYPE_RIGHTSLANT:
				if (end.x > start.x) {
					spriteBatch.draw(squarePartTexture, start.x, start.y
							- Constants.BALL_RAIDUS, 0, Constants.BALL_RAIDUS,
							squarePart.getLenth(), Constants.SQUAREPART_WIDTH,
							1, 1, -45, 0, 0, 16, 16, false, false);
				} else {
					spriteBatch.draw(squarePartTexture, end.x, end.y
							- Constants.BALL_RAIDUS, 0, Constants.BALL_RAIDUS,
							squarePart.getLenth(), Constants.SQUAREPART_WIDTH,
							1, 1, -45, 0, 0, 16, 16, false, false);
				}
				break;
			case BallMapSquarePart.TYPE_VERTICAL:
				if (end.y > start.y) {
					spriteBatch.draw(squarePartTexture, start.x
							- Constants.BALL_RAIDUS, start.y, 0, 0,
							Constants.SQUAREPART_WIDTH, squarePart.getLenth(),
							1, 1, 0, 0, 0, 16, 16, false, false);
				} else {
					spriteBatch.draw(squarePartTexture, end.x
							- Constants.BALL_RAIDUS, end.y, 0, 0,
							Constants.SQUAREPART_WIDTH, squarePart.getLenth(),
							1, 1, 0, 0, 0, 16, 16, false, false);
				}
			}
		}
	}

	// 渲染图片的操作
	private void renderRailBall(Simulation simulation) {
		RailBallContainer container = simulation.getCantainer();
		spriteBatch.enableBlending();
		spriteBatch.begin();
		for (int i = 0; i < container.size(); i++) {
			Ball ball = container.get(i);
			Log.i("x", ball.getPosition().x + ":" + ball.getPosition().y);
			spriteBatch.draw(redBallTexture, ball.getPosition().x
					- Constants.BALL_RAIDUS, ball.getPosition().y
					- Constants.BALL_RAIDUS, 0, 0, (int) Constants.BALL_WIDTH,
					(int) Constants.BALL_HEIGHT);
		}
		spriteBatch.end();
	}
}
